package com.wen.controller;

import com.wen.pojo.Books;
import com.wen.service.BookService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 20212
 */
@Controller
@RequestMapping("/book")
public class BookController {
    Logger logger = Logger.getLogger("BookController");
    //controller 调service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    /**
     * 查询全部的书籍，并且返回到一个书籍展示页面
     *
     * @param model
     * @return
     */

    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.queryAllBooks();

        model.addAttribute("list", list);
        return "allBook";
    }

    /**
     * 跳转到添加书籍页面
     *
     * @return
     */
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    /**
     * 添加书籍
     *
     * @return
     */

    @RequestMapping("/addBook")
    public String addBook(Books books) {
        System.out.println("addBook=>" + books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    /**
     * 跳转到添加书籍页面
     *
     * @return
     */
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(int id, Model model) {
        Books books = bookService.queryBooksById(id);
        model.addAttribute("books", books);
        return "updateBook";
    }

    /**
     * 修改书籍
     *
     * @param books
     * @return
     */
    @RequestMapping("/updateBook")
    public String updateBook(Books books) {
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook/id/{bookId}")
    public String deleteBook(@PathVariable int bookId) {
        bookService.deleteBookById(bookId);
        return "redirect:/book/allBook";

    }

    /**
     * 查询书籍
     */
    @RequestMapping("/queryBook")
    public String queryBook(String queryName, Model model) {
        if (queryName.length()==0) {
            model.addAttribute("error", "未查到概书籍");
        }
            logger.info("queryBook=>" + queryName);
            List<Books> list = bookService.queryBookByName(queryName);
            model.addAttribute("list", list);
            logger.info(list);

        return "allBook";
    }
}
